/**
 * 地址管理服务
 * 基于后端地址管理接口实现
 */

import http from './api.js'

// 地址管理服务
const addressService = {
    /**
     * 获取用户地址列表
     * GET /api/v1/user/addresses
     */
    getAddressList: async () => {
        try {
            const response = await http.get('/user/addresses', {}, {
                needAuth: true
            })

            return response
        } catch (error) {
            console.error('获取地址列表失败:', error)
            throw error
        }
    },

    /**
     * 添加收货地址
     * POST /api/v1/user/addresses
     */
    addAddress: async (addressData) => {
        try {
            const response = await http.post('/user/addresses', {
                name: addressData.name,
                phone: addressData.phone,
                province: addressData.province,
                city: addressData.city,
                district: addressData.district,
                address: addressData.address,
                isDefault: addressData.isDefault ? 1 : 0  // 转换为数字格式
            }, {
                needAuth: true,
                showLoading: true,
                loadingText: '添加中...'
            })

            return response
        } catch (error) {
            console.error('添加地址失败:', error)
            throw error
        }
    },

    /**
     * 更新收货地址
     * PUT /api/v1/user/addresses/{id}
     */
    updateAddress: async (addressId, addressData) => {
        try {
            const response = await http.put(`/user/addresses/${addressId}`, {
                name: addressData.name,
                phone: addressData.phone,
                province: addressData.province,
                city: addressData.city,
                district: addressData.district,
                address: addressData.address,
                isDefault: addressData.isDefault ? 1 : 0  // 转换为数字格式
            }, {
                needAuth: true,
                showLoading: true,
                loadingText: '更新中...'
            })

            return response
        } catch (error) {
            console.error('更新地址失败:', error)
            throw error
        }
    },

    /**
     * 删除收货地址
     * DELETE /api/v1/user/addresses/{id}
     */
    deleteAddress: async (addressId) => {
        try {
            const response = await http.delete(`/user/addresses/${addressId}`, {
                needAuth: true,
                showLoading: true,
                loadingText: '删除中...'
            })

            return response
        } catch (error) {
            console.error('删除地址失败:', error)
            throw error
        }
    },

    /**
     * 设置默认地址
     * PUT /api/v1/user/addresses/{id}/default
     */
    setDefaultAddress: async (addressId) => {
        try {
            const response = await http.put(`/user/addresses/${addressId}/default`, {}, {
                needAuth: true,
                showLoading: true,
                loadingText: '设置中...'
            })

            return response
        } catch (error) {
            console.error('设置默认地址失败:', error)
            throw error
        }
    },

    /**
     * 获取默认地址
     * GET /api/v1/user/addresses/default
     */
    getDefaultAddress: async () => {
        try {
            const response = await http.get('/user/addresses/default', {}, {
                needAuth: true
            })

            return response
        } catch (error) {
            console.error('获取默认地址失败:', error)
            throw error
        }
    },

    /**
     * 格式化地址显示
     */
    formatAddress: (address) => {
        if (!address) return ''

        return `${address.province}${address.city}${address.district}${address.address}`
    },

    /**
     * 验证地址数据
     */
    validateAddress: (addressData) => {
        const errors = []

        if (!addressData.name || addressData.name.trim() === '') {
            errors.push('收货人姓名不能为空')
        }

        if (!addressData.phone || addressData.phone.trim() === '') {
            errors.push('手机号码不能为空')
        } else {
            // 简单的手机号验证
            const phoneRegex = /^1[3-9]\d{9}$/
            if (!phoneRegex.test(addressData.phone)) {
                errors.push('请输入正确的手机号码')
            }
        }

        if (!addressData.province || addressData.province.trim() === '') {
            errors.push('请选择省份')
        }

        if (!addressData.city || addressData.city.trim() === '') {
            errors.push('请选择城市')
        }

        if (!addressData.district || addressData.district.trim() === '') {
            errors.push('请选择区县')
        }

        if (!addressData.address || addressData.address.trim() === '') {
            errors.push('详细地址不能为空')
        }

        return {
            isValid: errors.length === 0,
            errors
        }
    }
}

export default addressService
